#include "warehousing.h"
#include "ui_warehousing.h"

warehousing::warehousing(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::warehousing)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowShadeButtonHint);
    setFixedSize(this->width(),this->height());
    QSqlQuery sqlquery;
    sqlquery.prepare("SELECT stockid FROM stock_info");
    if(sqlquery.exec())
    {
        while(sqlquery.next())
        {
            QString id = sqlquery.value(0).toString();
            ui->comboBox->addItem(id);
        }
    }

}

warehousing::~warehousing()
{
    delete ui;
}



void warehousing::on_pushButton_warehousing_clicked()
{
    int number = ui->lineEditnumber->text().toInt();
    int goodsid = ui->comboBox->currentText().toInt();

    QSqlQuery sqlquery;
    sqlquery.prepare("UPDATE stock_info SET stockamount = stockamount + :number WHERE stockid = :goodsid");
    sqlquery.bindValue(":number",number);
    sqlquery.bindValue(":goodsid",goodsid);
    if(sqlquery.exec())
    {
        QMessageBox::information(this,"提示","入库成功",QMessageBox::Yes);
    }
    sqlquery.prepare("SELECT * FROM stock_info WHERE stockid = :id;");
    sqlquery.bindValue(":id",goodsid);
    if(sqlquery.exec())
    {
        while(sqlquery.next())
        {
            QString stockid = sqlquery.value(0).toString();
            QString stockname = sqlquery.value(1).toString();
            int amount = number;
            int stockprice = sqlquery.value(3).toInt();
            QString stocksupplice = sqlquery.value(4).toString();
            QString stockdirector = sqlquery.value(5).toString();
            QString stockwearhoustime = sqlquery.value(6).toString();
            QString stockremarks = sqlquery.value(8).toString();
            QSqlQuery sql;

            sql.prepare("INSERT INTO "
                             "inputdata(product_id,product_name,product_quantity,unit_price,supplier,responsible_person,input_time,remarks) "
                             "VALUES(:stockid,:stockname,:amount,:stockprice,:stocksupplice,:stockdirector,:stockwearhoustime,:stockremarks);");
            sql.bindValue(":stockid",stockid);
            sql.bindValue(":stockname",stockname);
            sql.bindValue(":amount",amount);
            sql.bindValue(":stockprice",stockprice);
            sql.bindValue(":stockdirector",stockdirector);
            sql.bindValue(":stocksupplice",stocksupplice);
            sql.bindValue(":stockwearhoustime",stockwearhoustime);
            sql.bindValue(":stockremarks",stockremarks);
            if(!sql.exec())
            {
                QMessageBox::information(this,"提示","插入失败",QMessageBox::Yes);
            }
        }
    }




}

void warehousing::on_pushButton_exit_clicked()
{
    close();
}
